package com.collection.web.controller;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.web.util.SavedRequest;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.collection.web.shiro.Menu;
import com.collection.web.shiro.ShiroUser;
import com.collection.web.shiro.util.ShiroUserUtils;

@Controller
public class LoginController {
	
	private static Logger logger = LoggerFactory.getLogger(LoginController.class);
	
	/**
	 * 登录
	 * @return
	 */
    @RequestMapping(value="login")
	public String login() {
    	if(ShiroUserUtils.isAuthenticated()){
    		return "main";
    	}else{
    		return "login";
    	}
	}
    
    /**
	 * ajax登录请求
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping(value="ajaxLogin",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> submitLogin(HttpServletRequest request,String username, String password) {
		Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
		Session session = SecurityUtils.getSubject().getSession();
		try {
			UsernamePasswordToken token = new UsernamePasswordToken(username, password);
			SecurityUtils.getSubject().login(token);
			ShiroUser principal = (ShiroUser)SecurityUtils.getSubject().getPrincipal();
			request.setAttribute("user",principal);
			request.setAttribute("username", username);
			resultMap.put("status", 200);
			
			//没有权限的上一次的请求URL
			//没有认证的用户请求需要认证的链接时，shiro在跳转前会把跳转过来的页面链接保存到session的attribute中，key的值叫shiroSavedRequest，我们可以能过WebUtils类拿到。
			SavedRequest savedRequest = WebUtils.getSavedRequest(request);
			logger.info("savedRequest:"+savedRequest);
			if(savedRequest!=null&&savedRequest.getRequestURI()!=null){
				System.out.println(" LoginController:-savedRequest.getRequestURI()");
				resultMap.put("redirect", savedRequest.getRequestURI());	
			}else{
				resultMap.put("redirect", "/main");
			}
			resultMap.put("message", "登录成功");

		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("status", 500);
			resultMap.put("message", e.getMessage());
		}
		return resultMap;
	}
	
	/**
	 * 首页访问
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="index")
	public String index(HttpServletRequest  request) {
		ShiroUser user=  ShiroUserUtils.getCurrentShiroUser();
		request.setAttribute("shiroUser", user);
        List<Menu> menusList = user.getMenus() ;
        request.setAttribute("menus", menusList);
        return "index";
	}
	
	@RequestMapping(value="main")
	public String main(HttpServletRequest  request) {
	    return "main";
	}
	
	@RequestMapping(value="left")
	public String left(HttpServletRequest  request) {
		ShiroUser user=  ShiroUserUtils.getCurrentShiroUser();
		List<Menu> menusList = user.getMenus() ;
		request.setAttribute("menus", menusList);
	    return "left";
	}
	
	@RequestMapping(value="top")
	public String top(){
	    return "top";
	}
}
